var FeeType = {

    selected_row_id:null,
    objGrid:null,

    init:function () {
        var self = this;

        self.objGrid = $('#flex').flexigrid({
            url:$('#table-url').val(),
            reload_after_add:true,
            dataType:'json',
            colModel:[
                {display:'ID', name:'id', width:20, sortable:true, align:'left', hide:true},
                {display:'Academic Year', name:'acc_year', width:100, sortable:true, align:'center', hide:false, editable:{form:'text', validate:'empty', maxlength:'9',defval:''}},
                {display:'Semester', name:'semester', width:100, sortable:true, align:'center', hide:false, editable:{form:'text', validate:'empty,integer', maxlength:'2' ,defval:''}},
                {display:'Session', name:'fee_code', width:100, sortable:true, align:'center', hide:false},
                {display:'Default Session', name:'set', width:150, sortable:true, align:'center', hide:false}
            ],
            formFields:[
                {type:'buttom', name:'New', bclass:'add', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Save', bclass:'save', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Edit', bclass:'edit', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Cancel', bclass:'cancel', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Set Default Session', bclass:'set', onpress:self.handleGridEvent}
            ],
            /*searchitems:[
             {display:'Proforma Number', name:'invoice_number', isdefault:true}
             ],*/
            checkboxSelection:true,
            editablegrid:{
                use:true,
                url:$('#table-editable-url').val()
            },
            columnControl:false,
            sortname:"id",
            sortorder:"desc",
            usepager:true,
            useRp:true,
            rp:15,
            showTableToggleBtn:false,
            height:300,
            subgrid:{
                use:false
            }
        });

    },

    handleGridEvent:function (com, grid, json) {
        if (com == 'New') {
            FeeType.objGrid.flexBeginAdd();
        }
        else if (com == 'Edit') {
            var row = FlexObject.getSelectedRows(grid);
            FeeType.objGrid.flexBeginEdit(row[0]);
        }
        else if (com == 'Save') {
            FeeType.objGrid.flexSaveChanges();
        }
        else if (com == 'Cancel') {
            FeeType.objGrid.flexCancel();
        }
        else if (com == 'Set Default Session') {
            if (FlexObject.rowSelectedCheck(FeeType.objGrid,grid)) {
                FeeType.set_default(grid);
            }
        }
    },

    set_default:function (grid) {
        var self = this;
        var url = $('#set_url').val();
        var row_ids = FlexObject.getSelectedRowIds(grid);
        self.do_set_session(url, row_ids[0]);
    },

    do_set_session: function(url,id) {
        var query = "id="+id;
        $.ajax({
            type: 'post',
            url: url,
            data: query,
            dataType: 'json',
            success: function(data) {
                if(data.code == 0){
                    alert(data.msg);
                    FeeType.objGrid.flexReload();
                }
                else{
                    alert(data.msg);
                }
            },
            error: function(t) {
                alert(t);
            }
        });
    }
};

/* when the page is loaded */
$(document).ready(function () {
    FeeType.init();
});